﻿namespace ChinaUnicom.Tasks
{
    using System;
    using System.Linq;
    using ChinaUnicom.Models;
    using Avatar.Schedule;
    using Avatar;
    using System.Net;

    public class ProductTask : ITask
    {
        const string PointName = "累计积分";

        public Product Product { get; private set; }

        public ProductTask()
        {
            Product = new Product();
        }

        public void Start()
        {
            if (Started != null)
            {
                Started(this, EventArgs.Empty);
            }

            Factory.Operation.RequestProduct(x =>
            {
                try
                {
                    if (x.Status == WebExceptionStatus.Success)
                    {
                        /*产品名称
                          生效时间
                          失效时间
                          产品描述*/

                        var items = Factory.Parser.Parse(x.Result, ItemType.Product);

                        foreach (var item in items)
                        {
                            switch (item.Name)
                            {
                                case "产品名称":
                                    Product.Name = item.Value;
                                    break;
                                case "生效时间":
                                    DateTime started;
                                    if (DateTime.TryParse(item.Value, out started))
                                    {
                                        Product.Started = started;
                                    }
                                    break;
                                case "失效时间":
                                    DateTime ended;
                                    if (DateTime.TryParse(item.Value, out ended))
                                    {
                                        Product.Ended = ended;
                                    }
                                    break;
                                case "产品描述":
                                    Product.Description = item.Value;
                                    break;
                            }
                        }

                        Context.Current.Account.LastUpdated = DateTime.Now; 

                        if (Completed != null)
                        {
                            Completed(this, EventArgs.Empty);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (Failed != null)
                    {
                        Failed(this, new FailedEventArgs(this, ex));
                    }
                }
            });
        }

        public int Delay { get { return 50; } }

        public bool IsUnique { get { return true; } }

        public event EventHandler Completed;

        public event EventHandler Started;

        public event EventHandler<FailedEventArgs> Failed;
    }
}
